package com.applancer.dataprovider;

import java.net.URLEncoder;

import android.content.Context;

import com.applancer.model.GeneralModel;
import com.applancer.model.UserModel;
import com.applancer.provider.NetworkChecking;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class DataProvider {

	protected String TAG = "Server connections";
	protected Context ct;

	public DataProvider(Context context) {
		ct = context;
	}

	public Boolean getAllMenu(AsyncHttpResponseHandler handler) {

		if (!NetworkChecking.checkConnection(ct))
			return false;

		AsyncHttpClient client = new AsyncHttpClient();

		String url = DataUtil.serviceURL + DataUtil.getallmenu;

		client.get(url, handler);

		return true;
	}

	public Boolean getFeed(String id, String offset,
			AsyncHttpResponseHandler handler) {
		if (!NetworkChecking.checkConnection(ct)) {
			return false;
		}

		AsyncHttpClient client = new AsyncHttpClient();

		String url = DataUtil.serviceURL + DataUtil.getarticlebyidcate
				+ "&idcate=" + id + "&offset=" + offset;

		client.get(url, handler);

		return true;
	}

	public Boolean login(String username, String pass,
			AsyncHttpResponseHandler handler) {

		if (!NetworkChecking.checkConnection(ct)) {
			return false;
		}
		AsyncHttpClient client = new AsyncHttpClient();
		String url = DataUtil.serviceURL;

		RequestParams params = new RequestParams();
		params.put(DataUtil.action, DataUtil.loginAction);
		params.put(DataUtil.username, username);
		params.put(DataUtil.userpass, pass);
		client.post(url, params, handler);

		return true;
	}

	public Boolean register(GeneralModel user, AsyncHttpResponseHandler handler) {

		if (!NetworkChecking.checkConnection(ct)) {
			return false;
		}

		UserModel model = (UserModel) user;

		AsyncHttpClient client = new AsyncHttpClient();
		String url = DataUtil.serviceURL;

		RequestParams params = new RequestParams();

		params.put(DataUtil.action, DataUtil.registerAction);
		params.put(DataUtil.username, model.getUserName());
		params.put(DataUtil.userpass, model.getPass());
		params.put(DataUtil.useremail, model.getEmail());
		params.put(DataUtil.userurl, model.getUrl());

		client.post(url, params, handler);

		return true;
	}

	public Boolean getComment(String iD, AsyncHttpResponseHandler handler) {

		if (!NetworkChecking.checkConnection(ct)) {
			return false;
		}

		AsyncHttpClient client = new AsyncHttpClient();

		String url = DataUtil.serviceURL + DataUtil.getallcommentbyidpost
				+ "&idpost=" + iD;

		client.get(url, handler);

		return true;
	}

	public Boolean postComment(String iD, String name, String email,
			String content, AsyncHttpResponseHandler handler) {
		if (!NetworkChecking.checkConnection(ct)) {
			return false;
		}

		AsyncHttpClient client = new AsyncHttpClient();

		String url = DataUtil.serviceURL + DataUtil.addcomment + "&idpost="
				+ iD + "@&authorname=" + name + "&authoremail=" + email
				+ "&content=" + content;

		url = url.replaceAll(" ", "%20");

		client.get(url, handler);

		return true;
	}

	public void getwoeid(double longitude, double latitude,
			AsyncHttpResponseHandler handler) {

		try {
			String url = "http://query.yahooapis.com/v1/public/yql?q= "
					+ URLEncoder.encode(
							"select * from geo.placefinder where text=\""
									+ longitude + "," + latitude
									+ "\" and gflags=\"R\"", "UTF-8")
					+ "&format=json";

			url = url.replace(" ", "%20");

			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, handler);
		} catch (Exception e) {

		}

	}

	public void getwoeidbyname(String city, AsyncHttpResponseHandler handler) {

		try {
			String url = "http://query.yahooapis.com/v1/public/yql?q="
					+ URLEncoder.encode(
							"select * from geo.places where text=\"" + city
									+ "\"", "UTF-8") + "&format=json";

			url = url.replace(" ", "%20");

			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, handler);
		} catch (Exception e) {

		}

	}

	public void getwoeidWithString(String search_name,
			AsyncHttpResponseHandler handler) {

	}

	public void getWeather(String woeid, AsyncHttpResponseHandler handler) {
		try {

			String url = "http://query.yahooapis.com/v1/public/yql?q="
					+ URLEncoder.encode(
							"select * from weather.forecast where woeid="
									+ woeid + " and u=\"c\" ", "UTF-8")
					+ "&format=json";

			url = url.replace(" ", "%20");

			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, handler);
		} catch (Exception e) {

		}
	}

	public Boolean getHomeData(AsyncHttpResponseHandler handler) {
		if (!NetworkChecking.checkConnection(ct)) {
			return false;
		}

		AsyncHttpClient client = new AsyncHttpClient();
		String url = DataUtil.serviceURL + DataUtil.homeData;

		client.get(url, handler);

		return true;
	}

	public Boolean getFeature(AsyncHttpResponseHandler handler, int offset) {
		if (!NetworkChecking.checkConnection(ct)) {
			return false;
		}

		AsyncHttpClient client = new AsyncHttpClient();
		String url = DataUtil.serviceURL + DataUtil.getpostfeature + "&offset="
				+ offset;

		client.get(url, handler);

		return true;
	}

	public Boolean getMostView(AsyncHttpResponseHandler handler) {
		if (!NetworkChecking.checkConnection(ct)) {
			return false;
		}

		AsyncHttpClient client = new AsyncHttpClient();
		client.setTimeout(15000);
		String url = DataUtil.serviceURL + DataUtil.getpostmostviewall;

		client.get(url, handler);

		return true;
	}

	public Boolean sendRegistrationIdToBackend(String regID,
			AsyncHttpResponseHandler handler) {
		if (!NetworkChecking.checkConnection(ct)) {
			return false;
		}

		AsyncHttpClient client = new AsyncHttpClient();
		String url = DataUtil.serviceURL + DataUtil.adddevicetoken + "&"
				+ DataUtil.registerAction + "=" + regID + "&" + DataUtil.type;

		client.get(url, handler);

		return true;
	}
}
